package com.yangtzeu.jwc.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Path;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.ConsoleMessage;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.loopj.android.http.PersistentCookieStore;
import com.yangtzeu.jwc.fragment.IndexFragment;
import com.yangtzeu.jwc.utils.ConfirmDialog;
import com.yangtzeu.jwc.utils.G;
import com.zhangdi.jwc.R;
import com.zjsx.lib.utils.L;
import com.zjsx.lib.utils.T;

import org.apache.http.cookie.Cookie;

/**
 * 显示详细的新闻
 */
public class NewsActivity extends BaseActivity {
    private WebView webView;
    private String title, content, url, fullUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setView() {
        setContentView(R.layout.activity_news);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        content = intent.getStringExtra("content");
        url = intent.getStringExtra("url");
        if (url == null) {
            finish();
        }
        if (url.startsWith("http")) {
            fullUrl = url;
        } else if (url.startsWith("www")) {
            fullUrl = "http://" + url;
        } else if (url.startsWith("/")) {
            fullUrl = G.NEW_JWC_HOME + url;
        } else {
            fullUrl = G.NEW_JWC_HOME + "/" + url;
        }
    }

    @Override
    public void initActionBar() {
        setTitle(title);
        setSimpleActionBar(null, title, null, null);
    }

    @Override
    public void initView() {
        super.initView();
        webView = (WebView) findViewById(R.id.webView_news);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        //自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //缓存
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        String appCachePath = getCacheDir().getAbsolutePath();
        webView.getSettings().setAppCachePath(appCachePath);

        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                setTitle(title);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent intent = new Intent();
                intent.putExtra("title", "正在加载网页...");
                intent.putExtra("url", url);
                intent.setClass(NewsActivity.this, NewsActivity.class);
                startActivity(intent);
                return true;
            }
        });


        PersistentCookieStore cookieStore  = new PersistentCookieStore(this);

        CookieManager.getInstance().setAcceptCookie(true);
        CookieManager.getInstance().removeAllCookie();
        for (Cookie cookie:cookieStore.getCookies()){
            String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
            L.d("cookieString:"+cookieString);
            CookieManager.getInstance().setCookie((cookie.isSecure() ? "https" : "http") + "://" + cookie.getDomain(), cookieString);
        }

        if (content != null) {
            webView.loadDataWithBaseURL(G.NEW_JWC_HOME, content, "text/html", "utf-8", null);
        } else {
            webView.loadUrl(fullUrl);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_news, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.open) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(fullUrl);
            intent.setData(content_url);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }
}
